#!/usr/bin/python2.5

import urllib, urllib2
url_login = 'http://localhost:8080/_ah/login?email=test%40example.com&admin=True&action=Login'
url_create_geotree = 'http://localhost:8080/admin/create_geotree'
url_add_points = 'http://localhost:8080/admin/add_points'


print """
login to localhost as admin"""

opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
urllib2.install_opener(opener)

f = opener.open(url_login)
data = f.read()
f.close()

print """
creating geotree
"""

f = opener.open(url_create_geotree)
data = f.readlines()
f.close()

print """
adding points
"""

do_add_points = True

while do_add_points:
    f = opener.open(url_add_points)
    data = f.readlines()
    f.close()
    for line in data:
        if 'Info:' in line:
            print line.replace('Info: ','')
            if ('no cities' in line) or ('GeoTree does not exist' in line): 
                do_add_points = False
        if 'Error:' in line:
            print line
            do_add_points = False

print """
updating tiles
"""

url_update_tiles = 'http://localhost:8080/admin/update_tiles'
do_update_tiles = True

while do_update_tiles:
    f = opener.open(url_update_tiles)
    data = f.readlines()
    f.close()
    for line in data:
        if 'Info:' in line:
            print line.replace('Info: ','')
            if ('nothing to update' in line) or ('GeoTree does not exist' in line): 
                do_update_tiles = False
        if 'Error:' in line:
            print line
            do_update_tiles = False

print ""
